package TreeDayShuZu;

public class Dome08 {
    public static void main(String[] args) {
        //用二分法进行快速排序
        int[] arr = {7, 23, 79, 81, 103, 127, 131, 147};
        System.out.println(binarySearch(arr,131));
    }
    public static int binarySearch(int[]arr,int number){
        //定义两个变量用来记录边界值
        int min=0;
        int max=arr.length-1;
        //使用循环变量不断去查找要查找的数据
        while (true){
            if (min>max){
                return-1;
            }
            int mid=(max+min)/2;
            if (arr[mid]>number){
                //min不变，变得是max
                max=mid-1;
            }else if (arr[mid]<number){
                //max不变，变得是min
                min=mid+1;
            }else {
                return mid;
            }
        }
    }
}
